package com.emip.servlet.baseinfo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


import com.emip.bean.ItemBean;
import com.emip.bean.MonitorTypeBean;
import com.emip.bean.QualityTypeBean;
import com.emip.config.SysParams;
import com.emip.pojo.Table;
import com.emip.service.ItemService;
import com.emip.service.MonitorTypeService;
import com.emip.service.QualityTypeService;
import com.emip.util.TableFormat;

@Controller
@RequestMapping(value="/base/qualityType")
public class QualityTypeAction {

	@Autowired
	QualityTypeService qualityTypeService;
	
	@Autowired
	MonitorTypeService monitorTypeService;
	
	@Autowired
	ItemService itemService;
	
	@RequestMapping(value="/management")
	public String index() {		
		return "/baseInfo/QualityTypeManagement";
	}
	
	@RequestMapping(value="/getAllQualityTypes")
	public @ResponseBody Table<QualityTypeBean> getAllQualityTypes() {
		List<QualityTypeBean> result = qualityTypeService.getAll();
		int records = result.size();
		int totalPages = records / SysParams.gridDefinedRowNumber + 1;
		return new TableFormat<QualityTypeBean>().formTable(totalPages, 1, records, result);
	}
	
	@RequestMapping(value="/getQualityTypeByID",method=RequestMethod.POST, headers="Accept=application/json")
	public @ResponseBody QualityTypeBean getQualityTypeByID(@RequestParam("eid") int eid) {
		return qualityTypeService.getQualityTypeByID(eid);
	}
	
	@RequestMapping(value="/addQualityType",method=RequestMethod.POST)
	public @ResponseBody QualityTypeBean addQualityType(@RequestParam("MTypeID")  int MTypeID, @RequestParam("itemID") String itemID,
			@RequestParam("name") String name, @RequestParam("MaxValue") double MaxValue, @RequestParam("MinValue") double MinValue,
			@RequestParam("remark") String remark, @RequestParam("qlevel") int qlevel) {
		MonitorTypeBean mBean = monitorTypeService.getMonitorTypeByID(MTypeID);
		ItemBean iBean = itemService.getItemByID(itemID);
		return qualityTypeService.addQualityType(mBean, name, iBean, MaxValue, MinValue, remark, qlevel);			
	}
	
	@RequestMapping(value="/updateQualityType",method=RequestMethod.POST)
	public @ResponseBody QualityTypeBean updateQualityType(@RequestParam("id") int id, @RequestParam("MTypeID")  int MTypeID, @RequestParam("itemID") String itemID,
			@RequestParam("name") String name, @RequestParam("MaxValue") double MaxValue, @RequestParam("MinValue") double MinValue,
			@RequestParam("remark") String remark, @RequestParam("qlevel") int qlevel) {
		MonitorTypeBean mBean = monitorTypeService.getMonitorTypeByID(MTypeID);
		ItemBean iBean = itemService.getItemByID(itemID);
		return qualityTypeService.updateQualityType(id, mBean, name, iBean, MaxValue, MinValue, remark, qlevel);		
	}
	
	@RequestMapping(value="/deleteQualityType",method=RequestMethod.POST)
	public @ResponseBody String deleteQualityType(@RequestParam("eID") int id) {
		qualityTypeService.deleteQualityType(id);
		return "success";
	}
}
